﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel.Composition;
using Core.Common;
using Core.Interfaces;
using Core.BusinessLogic.Interfaces;
using System.Web.Hosting;
using System.IO;
using SonCa.SocialNetwork.Web.Chat.Presenters;

namespace SonCa.SocialNetwork.Web.Files
{
    public partial class ReceiveFiles : System.Web.UI.Page
    {
        [Import]
        private IWebContext _webContext;        
        [Import]
        private IUploadService _uploadService;        

        protected void Page_Load(object sender, EventArgs e)
        {            
            MEFManager.Compose(this);
            if (_webContext.Files != null)
            {               
                int fileTypeID = 0;
                string filename = _webContext.Files[0].FileName;
                string extension = filename.Substring(filename.LastIndexOf(".") + 1);

                string[] arrayPhotos = {"jpg","gif","jpeg","png"};
                if (arrayPhotos.Contains(extension.ToLower()))
                    fileTypeID = 1;

                string[] arrayVideos = {"wmv","flv","swf","mp4","ogg","webm"};
                if (arrayVideos.Contains(extension.ToLower()))
                    fileTypeID = 2;

                string[] arrayAudios = { "wav", "mp3"};
                if (arrayAudios.Contains(extension.ToLower()))
                    fileTypeID = 3;

                if (fileTypeID != 0)
                {
                    string accountID = Request.QueryString["AccountID"];
                    string imgCover = Request.QueryString["ImgCover"];
                    Guid guidName = Guid.NewGuid();
                    if (fileTypeID == 1)
                        ReceiveFile.guiName = guidName.ToString() + "__s." + extension;
                    else if (fileTypeID == 2 || fileTypeID == 3)
                        ReceiveFile.guiName = guidName.ToString() + "__O";
                    _uploadService.UploadPhotos(guidName,fileTypeID, Convert.ToInt32(accountID), _webContext.Files, _webContext.AlbumID, imgCover);
                    
                    if (fileTypeID == 1 && imgCover != "1")
                    {                        
                        // Save image to temp folder
                        for (int i = 0; i < _webContext.Files.Count; i++ )
                        {
                            HttpPostedFile fi = _webContext.Files[i];
                            if (fi != null && fi.ContentLength > 0)
                            {
                                string folderTemp = HostingEnvironment.MapPath("~/Files/Photos/Temps");
                                fi.SaveAs(Path.Combine(folderTemp, fi.FileName));
                            }
                        }
                    }
                    if ((fileTypeID == 2 || fileTypeID == 3) && imgCover != "1")
                    {
                        // Save video to temp folder
                        for (int i = 0; i < _webContext.Files.Count; i++)
                        {
                            HttpPostedFile fi = _webContext.Files[i];
                            if (fi != null && fi.ContentLength > 0)
                            {
                                string folder = "Videos";
                                if (fileTypeID == 3)
                                    folder = "Audios";
                                string folderTemp = HostingEnvironment.MapPath("~/Files/"+ folder +"/Temps");
                                fi.SaveAs(Path.Combine(folderTemp, fi.FileName));
                            }
                        }
                    }
                }
            }
        }        
    }
}